import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { getShoppingCartAPI } from '@/api/cart'

export const useCartStore = defineStore(
  'cart',
  () => {
    const cartList = ref<any>([])
    const selectedCartList = computed(() => cartList.value.filter((item: any) => item.checked))

    const getCartList = async () => {
      const res: any = await getShoppingCartAPI()
      cartList.value = res.products
    }
    getCartList()

    return {
      cartList,
      selectedCartList,
      getCartList
    }
  },
  {
    persist: true
  }
)
